package Stragos.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;

import Stragos.excepcion.DAOExcepcion;
import Stragos.modelo.Evento;
import Stragos.modelo.Local;
import Stragos.modelo.Tipoproducto;
import Stragos.util.ConexionBD;


public class EventoDAO extends BaseDAO {

	public Evento ListarEventoPorId(int idEvento) throws DAOExcepcion {
		Evento ev = new Evento();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			String query = "select * from evento where idevento=?";
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setInt(1, idEvento);

			rs = stmt.executeQuery();
			if (rs.next()) {
				ev.setIdEvento(rs.getInt(1));
				ev.setNombre(rs.getString(2));
				ev.setFecha(rs.getString(3));
				ev.setCapacidad(rs.getInt(4));
				ev.setDescripcion(rs.getString(5));
				ev.setElog(rs.getBoolean(6));
				//ev.setIdLocal(rs.getInt(7));
				
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return ev;
	}
	//@WHP001-BEGIN
	public Collection<Evento> buscarPorNombre(String nombre)
			throws DAOExcepcion {
		String query = "select * from evento  where nombre like ?";
				
		Collection<Evento> c = new ArrayList<Evento>();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setString(1, "%" + nombre + "%");
			rs = stmt.executeQuery();
			while (rs.next()) {
				Evento ev = new Evento();
				
				ev.setIdEvento(rs.getInt("idevento"));				
				ev.setNombre(rs.getString("nombre"));
				ev.setFecha(rs.getString("fecha"));
				ev.setCapacidad(rs.getInt("capacidad"));
				ev.setDescripcion(rs.getString("descripcion"));
				ev.setElog(rs.getBoolean("elog"));
				//ev.setIdLocal(rs.getInt("id_local"));
				c.add(ev);
				
				
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return c;
	}

	public Evento insertar(Evento ev) throws DAOExcepcion {
		String query = "INSERT INTO evento(nombre,fecha,capacidad,descripcion,elog,idlocal) "
				+ "VALUES (?,?,?,?,?,?)";
		
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			
			stmt.setString(1, ev.getNombre());
			stmt.setString(2, ev.getFecha());
			stmt.setInt(3, ev.getCapacidad());
			stmt.setString(4, ev.getDescripcion());
			stmt.setBoolean(5, ev.isElog());
			stmt.setInt(6, ev.getIdLocal());
			
					
			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo insertar");
			}
			// Obtener el ultimo id
			int id = 0;
			query = "SELECT LAST_INSERT_ID()";
			stmt = con.prepareStatement(query);
			rs = stmt.executeQuery();
			if (rs.next()) {
				id = rs.getInt(1);
			}
			ev.setIdEvento(id);
			
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return ev;
	}
	//@WHP001-END
	public void eliminar(int idEvento) throws DAOExcepcion {
		String query = "DELETE FROM evento WHERE idevento=?";
		Connection con = null;
		PreparedStatement stmt = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setInt(1, idEvento);
			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo eliminar");
			}else{
				
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
	}

		
	public Evento modificar(Evento ev) throws DAOExcepcion {
		String query = "update evento set nombre=?, descripcion=? where idevento=?";
		Connection con = null;
		PreparedStatement stmt = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			
			stmt.setString(1, ev.getNombre());
			stmt.setString(2, ev.getDescripcion());
			stmt.setInt(3, ev.getIdEvento());
			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo actualizar");
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return ev;
	}
	
	public Collection<Evento> listarEventos()
			throws DAOExcepcion {
		String query = "select id_Evento, nombre, fecha, capacidad, descripcion, elog, Idlocal from evento ";
		Collection<Evento> c = new ArrayList<Evento>();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			rs = stmt.executeQuery();
			while (rs.next()) {
				Evento ev = new Evento();
				ev.setIdEvento(rs.getInt("idevento"));				
				ev.setNombre(rs.getString("nombre"));
				ev.setFecha(rs.getString("fecha"));
				ev.setCapacidad(rs.getInt("capacidad"));
				ev.setDescripcion(rs.getString("descripcion"));
				ev.setElog(rs.getBoolean("elog"));
				ev.setIdLocal(rs.getInt("id_local"));
				c.add(ev);
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return c;
	}
	

}
